﻿using FastReport.Map;
using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traceability.Commont.Domains;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.Domains.ScratchTheFat;
using static Butcher.winform.EnterpriseForms.ScaleBackfill;

namespace Butcher.winform.EnterpriseForms.MarketWeigh
{
    public partial class MarketWeighAdd : Form
    {
        public MarketWeighAdd()
        {
            InitializeComponent();
            getlist();
            textBox2.LostFocus += new EventHandler(textBox2_GotFocus);
            GetBulkSlaughter();
            getType();
            ApproachNos();
        }
        string BulkSlaughterNo1 = "";
        public void GetBulkSlaughter()
        {
            string url = @"http://localhost:5229/api/Batch/BulkSlaughterSearch";
            HttpClient client = new HttpClient();
            BulkSlaughter ctr = new BulkSlaughter();
            var sh = 0;
            var content = new StringContent(JsonSerializer.Serialize<BulkSlaughter>(ctr));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<BulkSlaughter> cts = aaa.ToObject<List<BulkSlaughter>>();
            UIFlowLayoutPanel[] appro = new UIFlowLayoutPanel[cts.Count];
            for (int t = 0; t < appro.Length; t++)
            {
                UIFlowLayoutPanel uiflow = new UIFlowLayoutPanel();
                uiflow.Size = new Size(180, 90);

                Button[] btn = new Button[appro.Length];
                btn[t] = new Button();
                btn[t].Size = new Size(100, 30);

                BulkSlaughter asas = cts[t];
                //转换为字符串
                btn[t].Name = Json.Serialize(asas.BulkSlaughterNo);
                //转换为实体
                BulkSlaughter babab = Json.Deserialize<BulkSlaughter>(btn[t].Name);
                btn[t].Text = "--获取--";

                for (int i = 0; i < 2; i++)
                {
                    Label newlabel = new Label();
                    Label newlabel2 = new Label();
                    newlabel.Width = 80;
                    newlabel2.Width = 80;
                    if (i == 0)
                    {
                        newlabel.Text = "商品批号:";
                        newlabel2.Text = cts[t].BulkSlaughterNo;
                    }
                    uiflow.Controls.Add(newlabel);
                    uiflow.Controls.Add(newlabel2);
                }
                btn[t].Click += new System.EventHandler(this.Button_Click);
                uiflow.Controls.Add(btn[t]);
                uiFlowLayoutPanel1.Controls.Add(uiflow);
            }
        }
        private void Button_Click(object sender, EventArgs e)
        {

            Button button = (Button)sender;
            BulkSlaughterNo1 = button.Name;
            MessageBox.Show(button.Name);
        }

        Scale ScaleInfo = new Scale();
        private void uiSymbolButton2_Click(object sender, EventArgs e)
        {
            this.Hide();
            ScaleBackfill.pid = 7;
            ScaleBackfill scale = new ScaleBackfill();
            scale.Owner = this;
            scale.Hide();
            scale.ShowDialog();
            Application.ExitThread();
        }

        public void getlist()
        {
            var strValue = ScaleBackfill.TxtVal;
            if (!string.IsNullOrEmpty(strValue))
            {
                ScaleInfo = Json.Deserialize<Scale>(strValue);
                textBox3.Text = ScaleInfo.TareWeightDeduction.ToString();
                uiSymbolButton2.Text = ScaleInfo.ScaleName;
                getStash(ScaleInfo.Warehouse);
            }
        }

        public void textBox2_GotFocus(object sender, EventArgs e)
        {
            textBox4.Text = (Convert.ToDecimal(textBox2.Text) - Convert.ToDecimal(textBox3.Text)).ToString();
        }
        /// <summary>
        /// 加载仓库下拉框
        /// </summary>
        /// <param name="id"></param>
        public void getStash(int id)
        {
            Stash stash = new Stash();
            string url = @"http://localhost:5229/api/Batch/StashList";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Stash>(stash));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Stash> enterprises = aaa.ToObject<List<Stash>>();
            enterprises = enterprises.Where(x => x.Stashid == id).ToList();
            this.cb_Level.DataSource = enterprises;
            this.cb_Level.DisplayMember = "StashName";
            this.cb_Level.ValueMember = "Stashid";
        }
        public void getType()
        {
            string url = @"http://localhost:5229/api/Enterprise/SearchClients";
            HttpClient client = new HttpClient();
            Clients ctr = new Clients();
            var sh = 0;
            var content = new StringContent(JsonSerializer.Serialize<Clients>(ctr));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Clients> cts = aaa.ToObject<List<Clients>>();
            this.uiComboBox2.DataSource = cts;
            this.uiComboBox2.DisplayMember = "ClientName";
            this.uiComboBox2.ValueMember = "ClientID";
        }
        public class approached
        {
            public int pageIndex { get; set; } = 1;

            public int pageSize { get; set; } = 50;
            public DateTime? ApproachTime { get; set; } = null;
            public string? Suppliers { get; set; } = null;
            public string? SettlementType { get; set; } = null;
            public int? ApproachStatus { get; set; } = 0;
        };
        public void ApproachNos()
        {
            int counts = 0;
            Approach approach = new Approach();
            approached approa = new approached();
            string url = @"http://localhost:5229/api/Enterprise/SelectApproachList";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<approached>(approa), Encoding.UTF8);
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject josnresult = JObject.Parse(result);
            var results = josnresult["lists"].Value<JArray>();
            var count = josnresult["totalCount"];
            counts = Convert.ToInt32(count);
            List<Approach> ssss = results.ToObject<List<Approach>>();
            this.uiComboBox1.DataSource = ssss;
            this.uiComboBox1.DisplayMember = "ApproachNo";
            this.uiComboBox1.ValueMember = "ApproachId";
        }
        int ShoppingId = 0;
        private void uiFlowLayoutPanel2_Load(object sender, EventArgs e)
        {
            Shopping shopping = new Shopping();
            string url = @"http://localhost:5229/api/Batch/getShop";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Shopping>(shopping));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Shopping> enterprises = aaa.ToObject<List<Shopping>>();


            Button[] buttons = new Button[enterprises.Count];
            for (int i = 0; i < buttons.Length; i++)
            {
                buttons[i] = new Button();
                //buttons[i].Text = ssss[i].DuctionName; /*(i + 1).ToString();*/
                buttons[i].Location = new Point(i * 130 + 130, 30);
                Shopping asas = enterprises[i];
                //转换为字符串
                buttons[i].Name = Json.Serialize(asas.ShoppingDesignationid);
                //转换为实体
                Shopping babab = Json.Deserialize<Shopping>(buttons[i].Name);
                buttons[i].Size = new Size(100, 80);
                buttons[i].BackColor = Color.White;
                buttons[i].ForeColor = Color.Black;
                //ScaleName.Text = ssss[i].ScaleName;
                buttons[i].Text = enterprises[i].ShoppingDesignationName;
                buttons[i].Click += new System.EventHandler(this.buttons_Click);
                uiFlowLayoutPanel2.Controls.Add(buttons[i]);
            }
        }
        private void buttons_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            ShoppingId = Convert.ToInt32(button.Name);
            MessageBox.Show(button.Name);
        }

        int ShoppingIds = 0;
        private void uiFlowLayoutPanel3_Load(object sender, EventArgs e)
        {
            Shopping shopping = new Shopping();
            string url = @"http://localhost:5229/api/Batch/getShop";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Shopping>(shopping));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Shopping> enterprises = aaa.ToObject<List<Shopping>>();


            Button[] buttons = new Button[enterprises.Count];
            for (int i = 0; i < buttons.Length; i++)
            {
                buttons[i] = new Button();
                //buttons[i].Text = ssss[i].DuctionName; /*(i + 1).ToString();*/
                buttons[i].Location = new Point(i * 130 + 130, 30);
                Shopping asas = enterprises[i];
                //转换为字符串
                buttons[i].Name = Json.Serialize(asas.ShoppingDesignationid);
                //转换为实体
                Shopping babab = Json.Deserialize<Shopping>(buttons[i].Name);
                buttons[i].Size = new Size(100, 80);
                buttons[i].BackColor = Color.White;
                buttons[i].ForeColor = Color.Black;
                //ScaleName.Text = ssss[i].ScaleName;
                buttons[i].Text = enterprises[i].ShoppingDesignationName;
                buttons[i].Click += new System.EventHandler(this.buttonsd_Click);
                uiFlowLayoutPanel3.Controls.Add(buttons[i]);
            }
        }
        private void buttonsd_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            ShoppingIds = Convert.ToInt32(button.Name);
            MessageBox.Show(button.Name);
        }

        SalesWeighing sale = new SalesWeighing();
        private void button24_Click(object sender, EventArgs e)
        {
            sale.ProductNo = BulkSlaughterNo1;
            sale.RepositoryID = Convert.ToInt32(cb_Level.SelectedValue);
            sale.ProductCode = ShoppingIds;
            sale.WeightNum = Convert.ToInt32(textBox5.Text);
            sale.Tare = Convert.ToDecimal(textBox3.Text);
            sale.NetWeight = Convert.ToDecimal(textBox4.Text);
            sale.AverageWeight = Convert.ToDecimal(textBox4.Text);
            sale.Remark = textBox7.Text;
            sale.ScaleId = ScaleInfo.Scaleid;
            sale.ApproachNo = uiComboBox1.Text;
            sale.ClientID = Convert.ToInt32(uiComboBox2.SelectedValue);
            sale.Adjust = ShoppingId;
            sale.WeigheTime = DateTime.Now;
            string url = @"http://localhost:5229/api/Ending/SalesWeighingCreate";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<SalesWeighing>(sale));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            if (info != null)
            {
                MessageBox.Show("新增成功");
            }
            else
            {
                MessageBox.Show("新增失败");
            }
        }

        
    }
}
